package com.itljm.cxrMarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itljm.cxrMarket.common.PageResult;
import com.itljm.cxrMarket.common.Result;
import com.itljm.cxrMarket.entity.SysPermission;
import com.itljm.cxrMarket.service.SysPermissionService;
import com.itljm.cxrMarket.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ljm
 * @date 2025/1/3 0:27
 */
@RestController
@RequestMapping("/sysPermission")
public class SysPermissionController {
    @Autowired
    private SysPermissionService sysPermissionService;

    // 分页查询权限列表
    @RequestMapping("/info")
    public PageResult<SysPermission> selectPermissionPage(Integer page, Integer limit, String desc) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 15;
        }
        Page<SysPermission> permissionPage = new Page<>(page, limit);
        QueryWrapper<SysPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("1",1);

        if (StringUtils.isNotEmpty(desc)) {
            wrapper.like("description", desc);
        }

        List<SysPermission> list = sysPermissionService.selectPermissionPage(permissionPage, wrapper);
        return new PageResult<>(list, permissionPage.getTotal());
    }

    // 添加或更新权限
    @RequestMapping("/save")
    public Result save(@RequestBody SysPermission sysPermission) {
        boolean b = sysPermissionService.addOrUpdate(sysPermission);
        if (b) {
            return Result.ok();
        }
        return Result.fail();
    }

    // 删除权限
    @RequestMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        boolean b = sysPermissionService.deleteById(id);
        if (b) {
            return Result.ok();
        }
        return Result.fail();
    }

    // 根据id查询权限详情
    @RequestMapping("/info/{id}")
    public Result detail(@PathVariable Long id) {
        SysPermission sysPermission = sysPermissionService.selectById(id);
        if (sysPermission != null) {
            return Result.ok(sysPermission);
        }
        return Result.fail();
    }
}
